package com.guacun.controller;

import com.guacun.entry.InfoManagerEntry;

import com.guacun.model.Teacher;

import com.guacun.service.TeacherService;

import java.util.Scanner;
//老师管理系统
public class TeacherController {
    private Scanner scanner = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    //老师管理系统菜单
    public void teacherMenu() {
        System.out.println("---------------------欢迎使用关村老师管理系统-----------------------");
        System.out.println("------1.添加老师    2.删除老师   3.修改老师  4.查询老师  5.back------");
        System.out.println("请输入您的选择:");
        while (true) {
            int i = scanner.nextInt();
            if (i == 1) {
                addinput();
            } else if (i == 2) {
                delete();
            } else if (i == 3) {
                update();
            } else if (i == 4) {
                selectall();
            } else if (i == 5) {
                System.out.println("退出成功");
                new InfoManagerEntry().mainMenu();
                break;
            } else {
                System.out.println("请重新输入您的选择:");
            }

        }
    }
    //添加
    public void addinput() {
        System.out.println("请输入老师id:");
        int id = scanner.nextInt();
        System.out.println("请输入老师姓名:");
        String name = scanner.next();
        System.out.println("请输入老师年龄:");
        int age = scanner.nextInt();
        System.out.println("请输入老师生日:");
        String birthday = scanner.next();
        int flag = teacherService.addTeacher(new Teacher(id, name, age, birthday));
        if (flag == 1) {
            teacherMenu();
        } else {
            System.out.println("添加失败，请重新添加");
            addinput();
        }
    }
//查询
    public void selectall() {
        int i = teacherService.selectTeacher();
        if (i == 1) {
            System.out.println("查询成功");
            teacherMenu();
        } else {
            System.out.println("查询失败");
            teacherMenu();
        }
    }
    //删除
    public  void  delete(){
        System.out.println("请输入老师id:");
        int id = scanner.nextInt();
       int i= teacherService.deleteTeacher(id);
       if (i==1){
           System.out.println("删除成功");
           teacherMenu();
       }else {
           System.out.println("删除失败");
           delete();
       }
    }
    //修改
    public  void  update(){
        System.out.println("请输入真实老师id:");
        int id = scanner.nextInt();
        Teacher teacher = teacherService.selectTeacherbyid(id);
        if (null!=teacher){
                int k = teacherService.updateTeacher(teacher.getId());
                if (k==1){
                    System.out.println("修改成功");
                    teacherMenu();
                }
            }else {
                System.out.println("id不存在!");
                update();
            }

    }
}
